package com.googlecode.continuity.plugin.codecomment.client;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.*;
import com.googlecode.continuity.plugin.codecomment.client.message.CodeCommentMessages;
import com.googlecode.continuity.plugin.gwtext.client.ContentBox;

/**
 * FileCommentListBox
 *
 * @author Peter Schmitt <aragos@aragos.de>
 */
public class FileCommentListBox extends ContentBox {
// ------------------------------ FIELDS ------------------------------

  private FlexTable content;
  private int lineColumn = 0;
  private int revisionColumn = 1;
  private int authorColumn = 2;
  private int contentColumn = 3;
  private int contentTruncateLength = 20;

  private CodeCommentSelectListener selectListener;

  private final CodeCommentMessages messages = (CodeCommentMessages) GWT.create(CodeCommentMessages.class);

// --------------------------- CONSTRUCTORS ---------------------------

  public FileCommentListBox(CodePointer fileInfo, CodeCommentSelectListener selectListener) {
    Label fileInfoLabel = new Label(messages.commentsForFile(fileInfo.getName()));
    content = new FlexTable();
    contentContainer.setWidget(content);
    frame.insert(fileInfoLabel, frame.getWidgetIndex(contentContainer));

    setText(messages.fileCommentListHeader());

    addStyleName("fileCommentList");
    content.addStyleName("content");
    fileInfoLabel.addStyleName("fileHeader");

    setSelectListener(selectListener);

    initHeaders();
  }

  private void initHeaders() {
    // use spans to fix IE wordwrap issue
    content.setWidget(0, lineColumn, new HTML("<span>" + messages.lineColumnHeader() + "</span>"));
    content.setWidget(0, revisionColumn, new HTML("<span>" + messages.revisionColumnHeader() + "</span>"));
    content.setWidget(0, authorColumn, new HTML("<span>" + messages.authorColumnHeader() + "</span>"));
    content.setWidget(0, contentColumn, new HTML("<span>" + messages.contentColumnHeader() + "</span>"));

    for (int i = 0; i < 4; i++) {
      content.getFlexCellFormatter().addStyleName(0, i, "contentHeader");
    }
  }

  public void setSelectListener(CodeCommentSelectListener selectListener) {
    this.selectListener = selectListener;
  }

// -------------------------- OTHER METHODS --------------------------

  public void addComments(List comments) {
    for (int i = 0; i < comments.size(); i++) {
      addComment((CodeComment) comments.get(i));
    }
  }

  public void addComment(final CodeComment comment) {
    int row = content.getRowCount();

    int firstLine = comment.getLocation().getFirstLine();
    int lastLine = comment.getLocation().getLastLine();
    Label lines = new Label(firstLine != lastLine ? firstLine + "-" + lastLine : firstLine + "");
    lines.addClickListener(new ClickListener() {
      public void onClick(Widget widget) {
        hide();
        selectListener.onSelect(comment);
      }
    });
    lines.addStyleName("lines");
    lines.addStyleName("link");
    content.setWidget(row, lineColumn, lines);
    Label version = new Label(comment.getLocation().getVersion() + "");
    version.addStyleName("version");
    content.setWidget(row, revisionColumn, version);
    Label author = new Label(comment.getAuthor().getUsername());
    author.addStyleName("author");
    content.setWidget(row, authorColumn, author);
    String message = comment.getContent().replaceAll("\n", " ");
    if (message.length() > contentTruncateLength) {
      message = message.substring(0, message.length() - 3) + "...";
    }
    Label content = new Label(message);
    content.addStyleName("message");
    this.content.setWidget(row, contentColumn, content);
  }
}
